<template>
  <div class="glwz_box">
    <div class="header_sele">
      <div class="add_qy">
        <a-button type="primary" @click="tjwz">添加文章</a-button>

        <a-button type="primary" @click="showModal">批量删除</a-button>
        <a-modal
          title="Title"
          :visible="visible"
          @ok="start"
          :confirmLoading="confirmLoading"
          @cancel="handleCancel"
        >
          <p>确认删除吗？</p>
        </a-modal>
      </div>
      <div>
        <div class="list_head">
          <div class="fl_right">
            <a-auto-complete
              :dataSource="fldatalist"
              style="width: 200px"
              placeholder="请选择分类"
              :filterOption="flfilterOption"
              @change="handleChange"
            />
          </div>
        </div>
     
        <div class="seach">
          <div class="sele">
            <a-input placeholder="请输入文章名称 " style="width: 250px" @change="onSearch" />
            <a-icon type="search" class="search" />
          </div>
        </div>
      </div>
    </div>
       <div class="gl_tjwz" v-if="$store.state.tjwz">
          <tjwzym class="gl_tjym"></tjwzym>
        </div>
    <div class="glwz">
      <a-table
        @change="handleChangelist"
        :loading="loading"
        :columns="columns"
        :pagination="pagination"
        :dataSource="data"
        :rowSelection="rowSelection"
      >
        <template slot="pic" slot-scope="text, record, index">
          <div>
            <img
              class="glwzpic"
              :src="`https://image.bearing.cn/qy/member/`+record.sqdwid+`/`+record.pic"
              alt
            />
          </div>
        </template>

        <template slot="operation" slot-scope="text, record, index">
          <div class="editable-row-operations">
            <a class="del" @click="()=>xiugai(record,record.key)">修改</a>
            <a-popconfirm class="del" title="确定删除吗?" @confirm="() => wzdelete(record,record.key)">
              <a>删除</a>
            </a-popconfirm>

            <a @click="()=>wzone(record,record.key)">置顶</a>
          </div>
        </template>
      </a-table>
    </div>
    <!-- 表格-================= -->
  </div>
</template>

<script>
import tjwzym from "../tjwz/tjwzym.vue";
const columns = [
  {
    title: "编号",
    dataIndex: "id_num"
  },
  {
    title: "图片",
    dataIndex: "pic",
    scopedSlots: { customRender: "pic" }
  },
  {
    title: "文章名称",
    dataIndex: "title"
  },

  {
    title: "分类名称",
    dataIndex: "articleName"
  },
  {
    title: "发布时间",
    dataIndex: "datetime"
  },
  {
    title: "操作",
    dataIndex: "operation",
    scopedSlots: { customRender: "operation" }
  }
];
//上传图片
// function getBase64(img, callback) {
//   const reader = new FileReader();
//   let file = [];
//   file = reader;
//   reader.addEventListener("load", () => callback(reader.result));
//   reader.readAsDataURL(img);
//   sessionStorage.setItem("file", reader);
// }
const data = [];
import qs from "qs";
import { setTimeout } from "timers";
export default {
  data() {
    this.cacheData = data.map(item => ({ ...item }));

    return {
      data,
      columns,
      sele: "",
      serchvalue: "",
      pagination: {
        defaultPageSize: 10,
        showTotal: total => `共 ${total} 条数据`,
        total: 0
      },
      loading: true,
      dataSource: [],
      xzfl: "",
      selectedRowKeys: [],
      visible: false,
      confirmLoading: false,
      fldatalist: []
    };
  },
  components: {
    tjwzym
    // xzl
  },
  methods: {
    //搜索框
    onSearch(e) {
      this.serchvalue = e.target.value;
      this.handleChangelist(this.pagination, this.serchvalue, this.xzfl, "");
    },

    //获取文章列表
    handleChangelist(pagination, khmc, articleClass, sorter) {
      this.axios
        .post(
          "wenzhang/AdminArticleList.php",
          qs.stringify({
            page: pagination.current,
            pageSize: pagination.defaultPageSize,
            sqdwid: this.$store.state.bbl_sqdwid,
            title: this.serchvalue,
            articleClass: this.xzfl,
            sortField: sorter != undefined ? sorter.field : "",
            sortOrder: sorter != undefined ? sorter.order : ""
          })
        )
        .then(res => {
          if (res.data.code == 303) {
            this.data = res.data.result.data;
            this.pagination.total = Number(res.data.result.total);
            this.loading = false;
            this.cacheData = this.data.map(item => ({
              ...item
            }));
            for (var i = 0; i < res.data.result.data.length; i++) {
              this.data[i].key = res.data.result.data[i].id_num;
            }
          } else {
            this.data = [];
            this.loading = false;
          }
        });
    },

    //删除客文章
    wzdelete(value, key) {
      const dataSource = [...this.data];
      this.data = dataSource.filter(item => item.key !== key);
      this.axios
        .post(
          "wenzhang/AdminArticleListDelete.php",
          qs.stringify({ id_num: value.id_num })
        )
        .then(res => {
          if (res.data.code == 303) {
            this.$notification["success"]({
              message: "成功",
              description: res.data.msg
            });
          } else {
            this.$notification["error"]({
              message: "失败",
              description: res.data.msg
            });
          }
        });
    },

    //文章置顶
    wzone(value, key) {
      this.axios
        .post(
          "wenzhang/AdminArticleListTop.php",
          qs.stringify({
            id_num: value.id_num
          })
        )
        .then(res => {
          if (res.data.code == 303) {
            this.loading = false;
            this.handleChangelist(this.pagination, this.serchvalue, this.xzfl);
            this.$notification["success"]({
              message: "成功",
              description: res.data.msg,
              duration: 1.5
            });
          } else {
            this.$notification["error"]({
              message: "失败",
              description: res.data.msg,
              duration: 1.5
            });
          }
        });
    },

    //确认批量删除
    showModal() {
      if (this.selectedRowKeys.length) {
        this.visible = true;
      } else {
        this.visible = false;
        this.$notification["error"]({
          message: "失败",
          description: "请先选择要删除的列表",
          duration: 1.5
        });
      }
    },
    handleCancel(e) {
      this.visible = false;
    },

    //批量删除
    start() {
      const newData = [...this.data];
      let selectedRowKeys = this.selectedRowKeys;
      this.axios
        .post(
          "wenzhang/AdminArticleListBatchDelete.php",
          qs.stringify({
            id_num: selectedRowKeys
          })
        )
        .then(res => {
          //更新数据成功
          if (res.data.code == 303) {
            this.loading = false;
            this.$notification["success"]({
              message: "成功",
              description: res.data.msg,
              duration: 1.5
            });
            selectedRowKeys.map(rowkeys => {
              this.data = this.data.filter(item => item.key !== rowkeys);
            });
            this.visible = false;
          } else {
            this.loading = false;
            this.visible = false;

            this.$notification["error"]({
              message: "失败",
              description: res.data.msg,
              duration: 1.5
            });
          }
        });
    },

    //打开添加文章面板
    tjwz() {
      this.$store.state.tjwz = true;
    },

    //文字修改
    xiugai(value, key) {
      console.log(value)
      this.$store.state.radiolang=value.language
      this.$store.state.id_num = value.id_num;
      this.$store.state.title = value.title;
      this.$store.state.content = value.content;
      this.$store.state.articleClass = value.articleClass;
      this.$store.state.pushTime = value.datetime;
      this.$store.state.source = value.source;
      this.$store.state.author = value.author;
      this.$store.state.articleRemark = value.articleRemark;
      this.$store.state.picList = Number(value.picList);
      this.$store.state.browserTitle = value.browserTitle;
      this.$store.state.browserKeyword = value.browserKeyword;
      this.$store.state.browserDescribe = value.browserDescribe;
      this.$store.state.glxgsqdwid = value.sqdwid;
      this.$store.state.annex = value.annex;
      this.$store.state.tjwzimgUrl = value.pic;

      this.$store.state.tjwz = true;
      this.$store.state.xiugaiflag = "123";
    },
    //文章分类列表
    handleChange(value) {
      this.xzfl = value;
      this.handleChangelist(this.pagination, this.serchvalue, this.xzfl);
    },

    //客户名称
    flfilterOption(input, option) {
      return (
        option.componentOptions.children[0].text
          .toUpperCase()
          .indexOf(input.toUpperCase()) >= 0
      );
    }
  },
  computed: {
    rowSelection() {
      const { selectedRowKeys } = this;
      return {
        onChange: (selectedRowKeys, selectedRows) => {
          this.selectedRowKeys = selectedRowKeys;
        }
      };
    }
  },
  created() {
    this.$store.commit("getwzfl");
      
    setTimeout(() => {
      if (this.$store.state.articleName.length) {
        for (var i = 0; i < this.$store.state.articleName.length; i++) {
          this.fldatalist.push(this.$store.state.articleName[i].articleName);
        }
      }
    }, 2000);
        // this.$children.articleName=this.fldatalist
        // this.$children.articleName=this.fldatalist
        // console.log(this.fldatalist)

    this.handleChangelist(this.pagination);
    this.$store.state.tjwz=false
    console.log(this.$children.articleName)

    // this.$store.state.articleName=this.fldatalist
  // console.log(this.$store.state.articleName)

  }
};
</script>

<style lang='less'>
.glwz_box {
  
  .gl_tjwz {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 999999999;
    height: 930px;
    .gl_tjym {
      position: absolute;
      width: 90%!important;
      left: 50%;
      transform: translateX(-50%);
    }
    .yychack {
      // width: 16%!important;
    }
  }
  .header_sele {
    position: absolute!important;
    top: 55px;
    left: 1% !important;
    z-index: 99;
    text-align: right;
    width: 98%!important;
    background-color: #fff;
    box-shadow: 5px 5px 5px #ccc;
    // height: 50px;
    padding: 10px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin:10px 0!important;
    .add_qy {
      // width: 50%;
      display: inline-block;
      text-align: left;
      padding: 10px 0;
      height: 50px !important;
      button {
        margin: 0 10px;
      }
      .ant-btn-primary {
        background-color: #1890ff !important;
        border: 0 !important;
      }
    }
    .seach {
      display: inline-block;
      .sele {
        position: relative;
        i {
          position: absolute;
          top: 10px;
          right: 8px;
        }
      }
    }
    .list_head {
      display: inline-block;
      .tit {
        font-size: 16px;
      }
      padding: 10px 15px;
      .fl_right {
        .margin {
          margin: 0 5px;
        }
        .xzl_btn:hover {
          border: 0 !important;
          color: #fff !important;
          background: #1890ff !important;
        }
      }
    }
  }
  .glwz {
    width: 98%;
    position: relative;
    top: 147px;
    left: 1%;
    background-color: #fff;
    border: 1px solid #ccc;
    .glwzpic {
      width: 50px;
      height: 50px;
    }
    .ant-table-thead > tr,
    .ant-table-tbody > tr {
      height: 50px !important;
    }
    .del {
    }
  }
}

.fyloading {
  position: absolute;
  width: 90% !important;
  border: 1px solid #91d5ff;
  background: rgba(255, 255, 255, 0.5);
  padding: 30px;
  height: 800px !important;
  z-index: 999999 !important;
  top: 108px;
  left: 201px;
  .ant-spin-blur {
    opacity: 1 !important;
  }
  .ant-spin-text {
    font-size: 18px !important;
  }

}
.ant-spin-nested-loading > div > .ant-spin {
  top: 100px !important;
}
.del {
  padding-right: 15px;
  color: #40a9ff;
  cursor: pointer;
}
@media (max-width: 1200px){
  .glwz_box .gl_tjwz .gl_tjym{
    width: 94%!important;
    top: 9px!important;
  }
    .glwz_box .ant-table-tbody > tr > td{
      font-size: .75rem;
    }
  .tjwz .tjwz_tab .tjwz_cg .cg_n .yybb .ant-checkbox-group{
    width: 85%;
  }
 
   .glwz_box .gl_tjwz  .yychack {
    // width: 20%!important;
  }
}
</style>
